package com.wpp.coder;

import com.wpp.serializer.Serializer;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

import java.util.List;

/**
 * @author by wpp25
 * @Classname RpcDecoder
 * @Description
 * @Date 2020/11/26 20:01
 */
public class RpcDecoder extends ByteToMessageDecoder {

    private Serializer serializer;

    private Class<?> clazz;

    public RpcDecoder( Class<?> clazz,Serializer serializer) {
        this.serializer = serializer;
        this.clazz = clazz;
    }

    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {
        int length = byteBuf.readInt();

        byte[] bytes = new byte[length];

        byteBuf.readBytes(bytes);
        Object deserialize = serializer.deserialize(clazz, bytes);

        list.add(deserialize);
    }
}
